<?php

namespace App\Http\Controllers\Common;

use App\Http\Classes\Lesson;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LessonController extends Controller
{
    //
    public function getLessonsByBook(Request $request)
    {
        try
        {
            if ( $request->has('book') )
            {
                $lesson = new Lesson();
                $lessons = $lesson->getLessonsByBookId($request->get('book'));
                if ( $lessons )
                {
                    return $this->response(1, '获取成功', $lessons);
                }
                else
                {
                    return $this->response(0, '无匹配数据', null, $lesson->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺少参数', 'book');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取所有课程信息
     * GET
     */
    public function getAllLessons()
    {
        try
        {
            $lesson = new Lesson();
            $lessons = $lesson->getAllLessons();
            if ( $lessons )
            {
                return $this->response(1, '获取成功', $lessons);
            }
            else
            {
                return $this->response(0, '无匹配数据', null, $lesson->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取某个课程的信息
     * GET
     * lesson(int)
     */
    public function getLessonById(Request $request)
    {
        try
        {
            if ( $request->has('lesson') )
            {
                $lesson = new Lesson( $request->get('lesson') );

                $lessonInfo = $lesson->getLesson();
                if ( $lessonInfo )
                {
                    return $this->response(1, '获取成功', $lessonInfo);
                }
                else
                {
                    return $this->response(0, '无匹配数据', null, $lesson->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', null);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
